package com.errands.cn.services;

import com.errands.cn.entity.User;
import com.errands.cn.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserService {

    @Autowired
    UserMapper userMapper;

    public User getUser(Integer userId){
       User user=  userMapper.selectByPrimaryKey(userId);
       return user;
    }

    public String getUserPassword(String account){
        return userMapper.userPassword(account);
    }

    public User getUserByAccount(String account){
       User user = userMapper.selectByAccount(account);
        return user;
    }

    @Transactional
    public void updateUser(User user)throws Exception{
       userMapper.updateByPrimaryKeySelective(user);
    }

    @Transactional
    public boolean insertUser(User user){
        try{
            int n = userMapper.insert(user);
            if (n > 0)
                return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    @Transactional
    public void deleteUser(Integer uid)throws Exception{
       userMapper.deleteByPrimaryKey(uid);
    }

    public PageInfo<User> getUserByPage(Integer pageNum, Integer pageSzie){
        PageHelper.startPage(pageNum,pageSzie);
        List<User> userList = userMapper.selectUserAll();
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        return pageInfo;
    }
}
